package com.bridgeintelligent.tag.webserver.security;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.lang.NonNull;

import java.util.Map;

public abstract class BeanFactoryAdapter implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public final void setApplicationContext( @NonNull ApplicationContext applicationContext ) throws BeansException {
        context = applicationContext;
    }

    public static void autowireBean( Object bean ) {
        context.getAutowireCapableBeanFactory().autowireBean( bean );
    }

    protected static boolean containsBean( String name ) {
        return context.containsBean( name );
    }

    protected static <T> T getBean( String name, Class<T> clazz ) {
        return context.getBean( name, clazz );
    }

    protected static <T> Map<String, T> getBeans( Class<T> clazz ) {
        return context.getBeansOfType( clazz );
    }
}
